import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:mall/components/image_network.dart';
import 'package:mall/core/models/business_type.dart';
import 'package:mall/core/services/new_api.dart';
import 'package:mall/screens/manage/business_level_screen.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 业务申请
class BusinessTypeScreen extends StatefulWidget {
  static const routeName = '/BusinessTypeScreen';
  @override
  _BusinessTypeScreenState createState() => _BusinessTypeScreenState();
}

class _BusinessTypeScreenState extends State<BusinessTypeScreen> {
  String _error;
  List<BusinessType> _list;

  @override
  void initState() {
    super.initState();
    _fetchBusinessType();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('业务申请'),
      ),
      body: _error == null && _list == null
          ? Center(
              child: CircularProgressIndicator(),
            )
          : _list == null
              ? Center(
                  child: Text('$_error'),
                )
              : _buildBody(),
    );
  }

  Widget _buildBody() {
    return SingleChildScrollView(
      padding: EdgeInsets.all(10.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('请选择您需要申请的业务'),
          SizedBox(height: 10.w),
          Wrap(
            runSpacing: 10.w,
            children: List.generate(_list.length, (index) => _buildItem(_list[index])),
          ),
        ],
      ),
    );
  }

  Widget _buildItem(BusinessType type) {
    return Container(
      padding: EdgeInsets.all(8.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(4.w),
      ),
      width: (375 - 10 * 2).w,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              ImageNetwork(url: '${type.typeImageUrl}', width: 30.w, height: 30.w),
              SizedBox(width: 11.w),
              Text('${type.businessName}', style: TextStyle(fontSize: 16.sp)),
              Spacer(),
              Column(
                children: [
                  Text('${type.maxLevelName}'),
                  Text('最高级别', style: TextStyle(fontSize: 10.sp)),
                ],
              ),
            ],
          ),
          Divider(height: 28.w),
          Row(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '您当前的级别：${type.curBusinessLevelName == null || type.curBusinessLevelName.isEmpty ? '无' : type.curBusinessLevelName}',
                    style: TextStyle(fontSize: 12.sp),
                  ),
                  SizedBox(height: 6.w),
                  if (type.endTime != null && type.endTime.isNotEmpty)
                    Text('到期日期：${type.endTime}', style: TextStyle(fontSize: 12.sp)),
                ],
              ),
              Spacer(),
              FlatButton(
                minWidth: 0,
                padding: EdgeInsets.zero,
                onPressed: type.enabled ? () => _onPressedItem(type) : null,
                child: Text('去申请'),
              ),
            ],
          )
        ],
      ),
    );
  }

  void _onPressedItem(BusinessType type) {
    Navigator.of(context).pushNamed(BusinessLevelScreen.routeName, arguments: type);
  }

  void _fetchBusinessType() async {
    final api = context.read<NewApi>();
    try {
      final list = await api.businessType();
      if (!mounted) {
        return;
      }
      setState(() {
        _list = list;
      });
    } on DioError catch (e) {
      if (!mounted) {
        return;
      }
      setState(() {
        _error = e.message;
      });
    }
  }
}
